const ChatroomModel = require('../models/chatroomModel');

class ChatroomController {
    // 获取所有聊天室列表
    static async getAllChatrooms(req, res) {
        try {
            const chatrooms = await ChatroomModel.getAllChatrooms();

            // 格式化返回数据
            const formattedChatrooms = chatrooms.map(room => ({
                roomID: room.roomID,
                img: room.img,
                title: room.title,
                intro: room.intro,
                maxUsersLength: room.maxUsersLength,
                isPlaying: Boolean(room.isPlaying),
                currentSong: room.songID ? {
                    id: room.songID,
                    name: room.songName,
                    time: room.songTime,
                    author: room.songAuthor,
                    picUrl: room.songPicUrl
                } : null
            }));

            res.json({
                code: 200,
                message: '获取聊天室列表成功',
                data: formattedChatrooms
            });
        } catch (error) {
            console.error('获取聊天室列表失败:', error);
            res.status(500).json({
                code: 500,
                message: '获取聊天室列表失败',
                error: error.message
            });
        }
    }

    // 获取单个聊天室信息
    static async getChatroomById(req, res) {
        try {
            const { roomId } = req.params;
            const room = await ChatroomModel.getChatroomById(roomId);

            if (!room) {
                return res.status(404).json({
                    code: 404,
                    message: '聊天室不存在'
                });
            }

            // 格式化返回数据
            const formattedRoom = {
                roomID: room.roomID,
                img: room.img,
                title: room.title,
                intro: room.intro,
                maxUsersLength: room.maxUsersLength,
                isPlaying: Boolean(room.isPlaying),
                currentSong: room.songID ? {
                    id: room.songID,
                    name: room.songName,
                    time: room.songTime,
                    author: room.songAuthor,
                    picUrl: room.songPicUrl
                } : null
            };

            res.json({
                code: 200,
                message: '获取聊天室信息成功',
                data: formattedRoom
            });
        } catch (error) {
            console.error('获取聊天室信息失败:', error);
            res.status(500).json({
                code: 500,
                message: '获取聊天室信息失败',
                error: error.message
            });
        }
    }

    // 获取聊天室歌曲列表
    static async getChatroomSongs(req, res) {
        try {
            const { roomId } = req.params;
            const songs = await ChatroomModel.getChatroomSongs(roomId);

            // 格式化返回数据
            const formattedSongs = songs.map(song => ({
                id: song.songID,
                name: song.songName,
                time: song.songTime,
                author: song.songAuthor,
                picUrl: song.songPicUrl
            }));

            res.json({
                code: 200,
                message: '获取聊天室歌曲列表成功',
                data: formattedSongs
            });
        } catch (error) {
            console.error('获取聊天室歌曲列表失败:', error);
            res.status(500).json({
                code: 500,
                message: '获取聊天室歌曲列表失败',
                error: error.message
            });
        }
    }

    // 添加歌曲到聊天室
    static async addSongToChatroom(req, res) {
        try {
            const { roomId } = req.params;
            const song = req.body;

            // 验证必要字段
            if (!roomId || !song || !song.id || !song.name || !song.author) {
                return res.status(400).json({
                    code: 400,
                    message: '缺少必要参数',
                    error: '请提供有效的聊天室ID和歌曲信息'
                });
            }

            // 检查聊天室是否存在
            const chatroom = await ChatroomModel.getChatroomById(roomId);
            if (!chatroom) {
                return res.status(404).json({
                    code: 404,
                    message: '聊天室不存在'
                });
            }

            // 添加歌曲到聊天室
            const result = await ChatroomModel.addSongToChatroom(roomId, song);

            res.json({
                code: 200,
                message: '添加歌曲成功',
                data: result
            });
        } catch (error) {
            console.error('添加歌曲到聊天室失败:', error);
            res.status(500).json({
                code: 500,
                message: '添加歌曲到聊天室失败',
                error: error.message
            });
        }
    }
}

module.exports = ChatroomController; 